package Stack;

import java.util.Stack;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-04
 * Time:20:06
 * Text：394. 字符串解码
 */
public class Text4 {
    public static String decodeString(String s) {
        Stack<StringBuffer> t1=new Stack<>();
        t1.push(new StringBuffer());
        Stack<Integer> t2=new Stack<>();

        int i=0,n=s.length();
        char[] arrs=s.toCharArray();
        while(i<n){
            if(arrs[i]>='0' && arrs[i]<='9'){
                int tmp=0;
                while(i<n && arrs[i]>='0' && arrs[i]<='9'){
                    tmp=tmp*10+(arrs[i]-'0');
                    i++;
                }
                t2.push(tmp);
            }
            else if(arrs[i]=='[')
            {
                i++;
                StringBuffer tmp=new StringBuffer();
                while(i<n && arrs[i]>='a' && arrs[i]<='z'){
                    tmp.append(arrs[i]);
                    i++;
                }
                t1.push(tmp);
            }else if(arrs[i]==']'){
                StringBuffer tmp=t1.pop();
                int k=t2.pop();
                while(k--!=0)
                {
                    t1.peek().append(tmp);
                }
                i++;
            }else{
                StringBuffer tmp=new StringBuffer();
                while(i<n && arrs[i]>='a' && arrs[i]<='z'){
                    tmp.append(arrs[i]);
                    i++;
                }
                t1.peek().append(tmp);
            }
        }
        return t1.pop().toString();
    }

    public static void main(String[] args) {
        System.out.println(decodeString("abc"));
        ;
    }
}
